<?php
    // +----------------------------------------------------------------------
    // | 海豚PHP框架 [ DolphinPHP ]
    // +----------------------------------------------------------------------
    // | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
    // +----------------------------------------------------------------------
    // | 官方网站: http://dolphinphp.com
    // +----------------------------------------------------------------------
    // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
    // +----------------------------------------------------------------------

    namespace app\store\validate;

    use app\common\utils\UtilEncryption;
    use app\payment\model\WalletModel;
    use app\store\model\GoodsModel;
    use app\store\model\OrderModel;
    use think\Validate;

    /**
     * Class Cart
     *
     * @package app\cms\validate
     */
    class Order extends Validate
    {

        // 定义验证规则
        protected $rule = [
            'order_sn'          => 'require',
            'point'             => 'require|number',
            'pay_type'          => 'require|pay_type|in_array',
            'goods_id'          => 'require|number|check_post_data',
            'goods_number'      => 'require|number',
            'address_id'        => 'require|number',
            'cart_ids|购物车ID'    => 'require',
            'status|状态'         => 'require|number|check_status',
            'pay_password|支付密码' => 'require|checkPayPassword',
        ];

        // 定义验证提示
        protected $message = [

        ];

        // 定义验证场景
        protected $scene = [
            'buycart' => ['cart_ids','address_id'],
            'buyone'  => ['goods_id','goods_number','address_id'],
            'start'   => [
                'pay_password',
                'order_sn' => 'require',
                'point'    => 'require|number|check_pay_data',
                'pay_type' => 'require|in:alipay,wxpay,wallet,product,point'
            ],
            'status'  => ['order_sn','status']
        ];

        /**
         * 下单前验证数据
         *
         * @param $rule
         * @param $value
         * @param $data
         *
         * @return bool|string
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        protected function check_post_data($rule,$value,$data)
        {

            $goods = GoodsModel::where(['id' => $data['goods_id']])->field('num,status')->find();
            if ($goods['num'] < $data['goods_number'])
            {
                return '库存不足';
            }
            if ($goods['status'] == 0)
            {
                return '商品已下架啦';
            }
            return true;

        }

        /**
         * @param $rule
         * @param $value
         * @param $data
         *
         * @return bool
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        protected function check_status($rule,$value,$data)
        {

            if (!in_array($data['status'],[
                OrderModel::STATUS_CANCEL,
                OrderModel::STATUS_YES_PAY,
                OrderModel::STATUS_DELIVER_SIGN
            ]))
            {
                return '状态无效！';
            }

            $Order = OrderModel::where(['order_sn' => $data['order_sn'],'buyer_id' => $data['member_id']])->find();
            if (!$Order)
            {
                return '订单不存在';
            }
            switch($data['status'])
            {
                case OrderModel::STATUS_USER_REFUND:
                    if ($Order['status'] < OrderModel::STATUS_YES_PAY)
                    {
                        return '订单未付款,不可申请退款！';
                    }
                    elseif ($Order['status'] > OrderModel::STATUS_YES_PAY)
                    {
                        return '订单已发货,不可申请退款！';
                    }
                    elseif ($Order['status'] == $data['status'])
                    {
                        return '已经提交退款申请,不可重复提交退款!';
                    }
                    else
                    {
                        return true;
                    }

                    break;
                case OrderModel::STATUS_DELIVER_SIGN:

                    return true;

                    if ($Order['status'] < OrderModel::STATUS_YES_PAY)
                    {
                        return '订单已发货,不可申请退款！';
                    }
                    elseif ($Order['status'] == $data['status'])
                    {
                        return '已经提交退款申请,不可重复提交退款!';
                    }
                    else
                    {
                        return true;
                    }

                    break;
                case OrderModel::STATUS_CANCEL:
                    if ($Order['status'] == 0)
                    {
                        return '订单已取消过了！';
                    }
                    return true;
                    break;
                default:
                    return '状态无效！';
                    break;
            }
            //status
            return '状态无效！';
        }

        /**
         * 验证支付
         *
         * @param $rule
         * @param $value
         * @param $data
         *
         * @return bool|string
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        protected function check_pay_data($rule,$value,$data)
        {

            $OrderModel = OrderModel::where(['order_sn' => $data['order_sn'],'buyer_id' => $data['member_id']])->find();
            $OrderModel['amount'] / 100;

            $WalletModel          = WalletModel::userwallet($data['member_id']);
            $WalletModel['money'] = $WalletModel['money'] / 100;

            if ($WalletModel['money_status'] == 0)
            {
                return '你的钱包已被冻结，请使用其他渠道支付！';
            }

            if ($WalletModel['point_status'] == 0)
            {
                return '积分被冻结,不可使用积分抵扣！';
            }

            if ($data['point'] * 100 > $WalletModel['point'])
            {
                return '你的积分已不足以用于抵扣！';
            }

            return true;
        }

        protected function check_pay_data_back($rule,$value,$data)
        {

            $OrderModel = OrderModel::where(['order_sn' => $data['order_sn'],'buyer_id' => $data['member_id']])->find();
            $OrderModel['amount'] / 100;

            $WalletModel          = WalletModel::userwallet($data['member_id']);
            $WalletModel['money'] = $WalletModel['money'] / 100;

            if ($WalletModel['money_status'] == 0)
            {
                return '你的钱包已被冻结，请使用其他渠道支付！';
            }

            if ($WalletModel['point_status'] == 0)
            {
                return '积分被冻结,不可使用积分抵扣！';
            }
            if ($data['point'] > $WalletModel['point'])
            {
                return '你的积分已不足以用于抵扣！';
            }

            return true;
        }

        /**
         * 自定义验证规则
         * 检查支付密码是否正确
         *
         * @param $value
         * @param $rule
         * @param $data
         *
         * @return bool|string
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        protected function checkPayPassword($value,$rule,$data)
        {

            return WalletModel::where(['pay_password' => UtilEncryption::encryptMd5($value)])->find() ? true : '支付密码不正确，请重新驶入';
        }
    }
